import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fwanandroid/model/api/WanAndroidService.dart';
import 'package:fwanandroid/model/api/exceptions/ApiException.dart';
import 'package:fwanandroid/provider/UserState.dart';
import 'package:fwanandroid/ui/MyColors.dart';
import 'package:fwanandroid/utils/ToastUtil.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: MyColors.leiMuBlue,
        title: Text("我", style: const TextStyle(color: Colors.white)),
      ),
      body: Consumer<UserState>(builder: (context, userState, child) {
        return Column(
          children: [
            Row(
              children: [
                Image.asset(
                  "assets/images/img.png",
                  width: 100,
                  height: 100,
                ),
                InkWell(
                    onTap: () {
                      if (userState.user == null) {
                        Navigator.of(context).pushNamed('/login');
                      }
                    },
                    child: buildUserWidget(context, userState)),
              ],
            ),
            Container(
                width: MediaQuery
                    .of(context)
                    .size
                    .width,
                decoration: BoxDecoration(
                    border: Border(bottom: BorderSide(color: Colors.black))),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text("开源协议", style: TextStyle(fontSize: 16)),
                )),
            Container(
                width: MediaQuery
                    .of(context)
                    .size
                    .width,
                decoration: BoxDecoration(
                    border: Border(bottom: BorderSide(color: Colors.black))),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text("第三方库", style: TextStyle(fontSize: 16)),
                )),
            buildLoginOut(context, userState)
          ],
        );
      }),
    );
  }

  Widget buildUserWidget(BuildContext context, userState) {
    return Text(
        userState.user == null ? "登录/注册" : "${userState.user?.username}",
        style: TextStyle(fontSize: 18));
  }

  Widget buildLoginOut(BuildContext context, UserState userState) {
    return Visibility(
      visible: userState.user != null,
      child: InkWell(
        onTap: () {
          EasyLoading.show(status: 'loading...');
          WanAndroidService.logout().then((_){
            userState.updateUser(null);
          }).catchError((error) {
            Logger().d("登出失败${error.toString()}");
            ToastUtil.showByError(error);
          }).whenComplete((){
            EasyLoading.dismiss();
          });
        },
        child: Container(
            width: MediaQuery
                .of(context)
                .size
                .width,
            decoration: BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.black))),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Text("退出", style: TextStyle(fontSize: 16)),
            )),
      ),
    );
  }
}
